package com.example.expandablelistview.thread;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.expandablelistview.R;

public class Handler_runOnUiThreadEx extends Activity {
	private TextView mMainTv;
	private TextView mBackTv;
	
	private int mMainValue;
	private int mBackValue;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.handlerex);
		
		mMainTv = (TextView) findViewById(R.id.m_main_value);
		mBackTv = (TextView) findViewById(R.id.m_back_value);
		
		Thread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();
	}
	
	public void mOnClick(View v){
		mMainValue++;
		mMainTv.setText(String.format(getResources().getString(R.string.subTitle_thread_handler_mainValue), mMainValue));
	}
	

	class BackThread extends Thread{
		@Override
		public void run() {
			super.run();
			while(true){
				mBackValue++;
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						mBackTv.setText(String.format(getResources().getString(R.string.subTitle_thread_handler_backValue), mBackValue));						
					}
				});
				try{Thread.sleep(1000);}catch(InterruptedException e){}
			}
		}
	}
}
